﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;


namespace FYPDemo
{
    public partial class WebForm7 : System.Web.UI.Page
    {
        private void uploadFile(HttpPostedFile theFile, string uploadTo, string filename)
        {
            if (theFile != null && theFile.ContentLength > 0)
            {
                if (!Directory.Exists(Server.MapPath("~/" + uploadTo)))
                    Directory.CreateDirectory(Server.MapPath("~/" + uploadTo));

                string fname = filename + Path.GetExtension(Path.GetFileName(theFile.FileName));
                theFile.SaveAs(Server.MapPath(Path.Combine("~/" + uploadTo + "/", fname)));

            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindDDLCategory();
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            string prodName = "TempProduct";
            string uploadTo = "Product";

            if (FileUpload1.HasFile)
            {
                if (Directory.Exists(Server.MapPath("~/" + uploadTo + "/" + prodName)))
                {
                    System.IO.File.Delete(Server.MapPath("~/" + uploadTo + "/" + prodName));
                }

                else
                {
                    HttpPostedFile theFile = FileUpload1.PostedFile;
                    Session["PostedFile"] = theFile;
                    uploadFile(theFile, uploadTo, prodName);
                    ImgUpload.ImageUrl = "~/" + uploadTo + "/" + prodName + ".jpg";
                }
            }
        }

        protected void bindDDLCategory()
        {
            DataTable dt = ProductManagement.bindProductCategory();

            ddlCategory.DataSource = dt;
            ddlCategory.DataTextField = "Value";
            ddlCategory.DataValueField = "CategoryID";
            ddlCategory.DataBind();
        }

        protected void btnAddproduct_Click(object sender, EventArgs e)
        {
            int result = 0;
            int safeLevel = 0;
            string vendor = Session["username"].ToString();

            string vendorCode = ProductManagement.getVendorCode(vendor);

            if (vendorCode != "")
            {                
                string prodName = tbProdName.Text;
                double price = Convert.ToDouble(tbPrices.Text);
                decimal cost = Convert.ToDecimal(tbCost.Text);
                string img = ImgUpload.ImageUrl.ToString();
                string category = ddlCategory.SelectedItem.ToString();
                string name = Session["username"].ToString();
                string desc = textArea1.Text;
                int quantity = Convert.ToInt32(tbProdQuantity.Text);

                if (tbSafeLevel.Text != "")
                {
                    safeLevel = Convert.ToInt32(tbSafeLevel.Text);
                }
                
                string prefix = "";
                string runNo = "";
                int runningNo = 0;

                string uploadTo = "Product/" + category;

                if (prodName != "" && img != "" && category != "")
                {
                    System.IO.File.Delete(Server.MapPath(img));
                    HttpPostedFile theFile = Session["PostedFile"] as HttpPostedFile;
                    uploadFile(theFile, uploadTo, prodName);
                    ImgUpload.ImageUrl = "~/" + uploadTo + "/" + prodName + ".jpg";
                    img = ImgUpload.ImageUrl.ToString();
                }

                string productID = ProductManagement.getLatestProductID(name, category);

                if (productID != "")
                {
                    string num = productID.Remove(0, 5);
                    prefix = productID.Substring(0, 5);
                    runningNo = Convert.ToInt32(num) + 1;
                }

                else
                {
                    runningNo = 1;

                    if (category == "Clothes")
                    {
                        prefix = vendorCode + "C";
                    }

                    else if (category == "Furniture")
                    {
                        prefix = vendorCode + "F";
                    }

                    else if (category == "Pet")
                    {
                        prefix = vendorCode + "P";
                    }
                }

                runNo = String.Format("{0:00000}", runningNo);
                productID = prefix + runNo;

                result = ProductManagement.addNewProduct(productID, prodName, price, img, category, name, quantity, safeLevel, cost, desc);

                if (result > 0)
                {
                    MessageBox.Show("Successfully added the new product " + prodName);
                }

                else
                {
                    MessageBox.Show("There are some errors occur during the add new product process. Please try again later or contact our support team.");
                }
            }

            else
            {
                MessageBox.Show("You are required to update your information to add a unique vendor code first before process to add new product.");
            }

        }
    }
    }
